<?php

/**
 * 系统类
 */
final class System {
	//调用系统类文件
	public static function load_sys_class($class_name='',$module='sys',$new=true){
	
			static $classes = array();
			$path=self::load_class_file_name($class_name,$module);
			$key=md5($class_name.$path);
			if (isset($classes[$key])) {
				return $classes[$key];
			}
			if(file_exists($path)){
				include_once $path;
				if($new){
					$classes[$key] = new $class_name;
				}else{					
					$classes[$key]=true;
				}
				return $classes[$key];
			}else{
				_error('未找到class');
			}
				
	}
	//调用模块应用类
	public static function load_app_class($class_name='',$module='',$new=true){
			return self::load_sys_class($class_name,$module,$new);
	}

	public static function load_class_file_name($class_name='',$module='sys'){
		static $filename = array();
		if(isset($filename[$module.$class_name])) return $filename[$module.$class_name];
		if($module=='sys'){
		 	$filename[$module.$class_name]=BASE_DIR.'lib/'.$class_name.'.class.php';
		}else{
			$filename[$module.$class_name]=BASE_DIR.'class/'.$module.'/'.$class_name.'.class.php';
		}
		return $filename[$module.$class_name];
	}
	
	//函数文件名和所在模块
	public static function load_fun($fun_name){
		static $funcs = array();
		$path=BASE_DIR.'func/'.$fun_name.'.fun.php';
		$key = md5($path);
		if (isset($funcs[$key])) return true;
		if (file_exists($path)){
			$funcs[$key] = true;
			return include $path;
		}else{
			$funcs[$key] = false;
			_error('未找到方法');
		}
	}
	public static function CreateApp(){
		self::load_fun('global');
		self::load_sys_class('base','sys',false);
		$m = empty($_GET['module']) ? ROUTE_M : $_GET['module'];
		$c = empty($_GET['controller']) ? ROUTE_C : $_GET['controller'];
		$a = empty($_GET['action']) ? ROUTE_A : $_GET['action'];
		$cls = System::load_app_class($c,$m);
		if(method_exists($cls,$a)){
			unset($_GET['module']);
			unset($_GET['controller']);
			unset($_GET['action']);
			$get = empty($_GET) ? array() : $_GET;
			$post = empty($_POST) ? array() : $_POST;
			$request = empty($_REQUEST) ? array() : $_REQUEST;
			call_user_func(array($cls, $a),$get,$post,$request);
		}else{
			_error("未找到方法");
		}
	}
}

?>